package com.github.postsanf.yinian.adapter;

import android.content.Context;
import android.content.Intent;
import android.net.Uri;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.facebook.drawee.view.SimpleDraweeView;
import com.github.postsanf.yinian.R;
import com.github.postsanf.yinian.activity.AiteSelectActivity;
import com.github.postsanf.yinian.bean.YNAite;
import com.github.postsanf.yinian.utils.ImageOptHelper;

import java.util.List;

public class AitesAdapter extends RecyclerView.Adapter<AitesAdapter.ViewHolder> {
    private Context mContext;
    private List<YNAite> datas;

    public AitesAdapter(Context mContext, List<YNAite> datas) {
        this.mContext = mContext;
        this.datas = datas;
    }

    public class ViewHolder extends RecyclerView.ViewHolder {
        SimpleDraweeView iv_album_grid_item_image;                        //成员头像
        TextView name;
        ImageView id_item_select;

        public ViewHolder(View itemView) {
            super(itemView);
            iv_album_grid_item_image = (SimpleDraweeView) itemView
                    .findViewById(R.id.iv_album_grid_item_image);
            name = (TextView) itemView
                    .findViewById(R.id.name);
            id_item_select = (ImageView) itemView
                    .findViewById(R.id.id_item_select);
        }

        public void bindData(final YNAite listItems) {
            if (listItems != null) {
                Uri avatarUri = Uri.parse(listItems.getUpic());
                ImageOptHelper.showFrescoAvatar(avatarUri, iv_album_grid_item_image, mContext);
                name.setText(listItems.getUnickname());
                if (checkisExist(listItems.getUnickname())) {
                    id_item_select.setImageResource(R.drawable.image_selected_after);
                    id_item_select.setTag(true);
                } else {
                    id_item_select.setImageResource(R.drawable.image_selected_before);
                    id_item_select.setTag(false);
                }
                id_item_select.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        boolean tag = (boolean) id_item_select.getTag();
                        if (tag) {
                            id_item_select.setTag(false);
                            id_item_select.setImageResource(R.drawable.image_selected_before);
                            Intent pintent = new Intent("deletephonetag");
                            pintent.putExtra("delphonename", listItems.getUnickname());
                            mContext.sendBroadcast(pintent);
                        } else {
                            id_item_select.setTag(true);
                            id_item_select.setImageResource(R.drawable.image_selected_after);
                            Intent pintent = new Intent("updatephonetag");
                            pintent.putExtra("phonename", listItems.getUnickname());
                            mContext.sendBroadcast(pintent);
                        }

                    }
                });
            }
        }

    }

    public boolean checkisExist(String Unickname) {
        for (int i = 0; i < AiteSelectActivity.phoneNames.size(); i++) {
            if (Unickname.equals(AiteSelectActivity.phoneNames.get(i))) {
                return true;
            }
        }
        return false;
    }

    private YNAite getItem(int position) {
        return datas.get(position);
    }

    @Override
    public AitesAdapter.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        final View convertView = LayoutInflater.from(mContext).inflate(R.layout.yn_aite_item, parent, false);
        return new AitesAdapter.ViewHolder(convertView);
    }

    // 绑定数据到View
    @Override
    public void onBindViewHolder(AitesAdapter.ViewHolder holder, int position) {
        YNAite listItems = getItem(position);
        holder.bindData(listItems);
        holder.itemView.setTag(listItems);
    }

    @Override
    public int getItemCount() {
        return datas.size();
    }

}
